package sadmin

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"shop/models/sadmin"
)

type CompanyController struct {
	beego.Controller
}
//@Title companyshow
//@Description show company
//@Param name query string false "name"
//@Param code query string false "code"
//@Success 200 {object} sadmin.Company
//@router /company [get]
func (c *CompanyController)CompanyShow(name string,code string)  {

	com,err:=sadmin.CompanyShow(name,code)
	if err!=nil{
		c.Data["json"]="false"
	}else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]=com
	}
	c.ServeJSON()
}


//@Title companyshowbyid
//@Description 展示某个公司信息
//@Param id query int true "id"
//@Success 200 {object} sadmin.Company
//@router /companybyid/ [get]
func (c *CompanyController)CompanyShowById(id int)  {
	com:=sadmin.CompanyShowById(id)
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]=com
	c.ServeJSON()
}

//@Title companyadd
//@Description add company
//@Param body body sadmin.Company true "d"
//@Success 200 ok
//@router / [post]
func (c *CompanyController)CompanyAdd()  {
	var com sadmin.Company
	json.Unmarshal(c.Ctx.Input.RequestBody,&com)
		_,err:=sadmin.CompanyAdd(com)
		if err!=nil{
			c.Ctx.ResponseWriter.WriteHeader(201)
			c.Data["json"]=err.Error()
		}else {
			c.Ctx.ResponseWriter.WriteHeader(200)
			c.Data["json"]="ok"
		}
	c.ServeJSON()
}
// @Title companydel
// @Description 删除企业信息
// @Param	id		path 	int	true		"The id of company"
// @Success 200 ok
// @router /companydel/:id [delete]
func (c *CompanyController)CompanyDel()  {
	idd,_:=c.GetInt(":id")
	err:=sadmin.CompanyDel(idd)
	if err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=err.Error()
	}else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]="ok"
	}
	c.ServeJSON()
}
//@Title AdminCompanyedit
//@Description edit
//@Param body body sadmin.Company true "d"
//@Success 200 ok
//@router /companyedit [put]
func (c *CompanyController)CompanyEdit()  {
	var com sadmin.Company
	json.Unmarshal(c.Ctx.Input.RequestBody,&com)
	err:=sadmin.CompanyEdit(com)
	if err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=err.Error()
	}else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]=com
	}
	c.ServeJSON()
}

//@Title companyname
//@Description 企业管理时展示的可见企业列表，以及账号管理时的企业列表，超管可用
//@Success 200 {object} models.Company
//@router /companyname [get]
func (c *CompanyController)CompanyName()  {

	com:=sadmin.AllCompany()

		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]=com
	    c.ServeJSON()
}